package cn.thornbird.orgsync.controller;

import cn.thornbird.orgsync.annotation.CurrentUser;
import cn.thornbird.orgsync.exception.BadRequestException;
import cn.thornbird.orgsync.exception.ForbiddenException;
import cn.thornbird.orgsync.exception.NotFoundException;
import cn.thornbird.orgsync.model.PageList;
import cn.thornbird.orgsync.model.Result;
import cn.thornbird.orgsync.model.UserData;
import cn.thornbird.orgsync.model.combined.CompanyObject;
import cn.thornbird.orgsync.service.CompanyService;
import cn.thornbird.orgsync.vo.record.CompanyRecord;
import cn.thornbird.orgsync.vo.request.CompanyQueryRequest;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "/api/company")
@Api
@AllArgsConstructor
public class CompanyController {

    private final CompanyService companyService;

    @GetMapping("find/{id}")
    public Result<CompanyObject> find(@PathVariable String id) throws NotFoundException {
        return Result.success(companyService.find(id));
    }

    @GetMapping("query")
    public Result<PageList<CompanyObject>> query(@CurrentUser @ApiIgnore UserData userData,
                                                 CompanyQueryRequest companyQueryRequest,
                                                 Integer pageNumber, Integer pageSize) {
        return Result.success(companyService.query(userData, companyQueryRequest, pageNumber, pageSize));
    }

    @PostMapping("save")
    public Result<CompanyObject> save(@CurrentUser @ApiIgnore UserData userData,
                                   @RequestBody @Valid CompanyRecord companyRecord)
            throws BadRequestException, NotFoundException, ForbiddenException {
        return Result.success(companyService.save(userData, companyRecord));
    }

    @DeleteMapping("delete/{id}")
    public Result<CompanyObject> delete(@CurrentUser @ApiIgnore UserData userData, @PathVariable String id)
            throws BadRequestException, NotFoundException, ForbiddenException {
        return Result.success(companyService.delete(userData, id));
    }

}
